package com.kd.crawler.main;

import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.kd.crawler.common.jms.ActiveMQProducer;
import com.kd.crawler.common.utils.PropertiesUtils;
import com.kd.crawler.dao.crawler.CrawlerEntryDao;
import com.kd.crawler.entity.CrawlerEntry;

public class EntryMain {
	
	private static final int REST_TIME = 3000;
	private static final int LIMIT = 200;
	
	private static Logger logger=Logger.getLogger(EntryMain.class);
	
	private static Set<Integer> jobIds = new HashSet<Integer>();
	
	public static void main(String[] args) {
		Properties properties = PropertiesUtils.getProperties("activeMQ.properties");
//		int indexStart = 2053219;
//		int indexStart = 2504547;
		int indexStart = 0;
		while(true) {
			
			List<CrawlerEntry> list=CrawlerEntryDao.getInstance().queryInside(indexStart, LIMIT);
			logger.info("本次查询的入口数量为:"+list==null?0:list.size());
			if(null==list || list.size()==0) {
				// break to once circle.
//				try {
//					Thread.sleep(REST_TIME);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				indexStart = 0;
//				continue;
				break;
			}
			for(CrawlerEntry cu : list) {
				cu.setDetailed(false);
				try{
					
					if(jobIds.contains(cu.getJobID())) {
						continue;
					}
					logger.info("正将入口发送到队列中:"+JSON.toJSONString(cu));
					ActiveMQProducer.sendQueueMessage(properties.getProperty("QUEUE_CRAWL_ENTRY_MESSAGE"), JSON.toJSONString(cu));
					jobIds.add(cu.getJobID());
//					CrawlerEntryDao.getInstance().update(true, cu.getEntryId());
				}catch(Exception e){
					logger.error("发送入口到队列中报错:"+JSON.toJSONString(cu));
				}
			}
			indexStart = list.get(list.size()-1).getEntryId();
		}
	}
	
}
